import axios from 'axios';
import qs from 'qs'
import Config from '../../config/config'
// axios.defaults.timeout = 20000;
axios.defaults.baseURL = '';

if (process.env.NODE_ENV == 'development') {
  //本地
  axios.defaults.baseURL = "/proxy";
} else {
  //测试
  axios.defaults.baseURL = Config.API_url//内网
}


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise} 'Accept':'application/json;charset=UTF-8'
 */
export function fetch(url, data = {}) {
  return new Promise((resolve, reject) => {
    data.data_scope = sessionStorage.getItem("data_scope")
    axios({
      url: url,
      method: 'get',
      params: data,
      headers: {'Authorization': sessionStorage.getItem("token"), 'Content-Type': 'application/json;charset=utf-8'},
    }).then(response => {
      if (response.data.status == 400001) {
        window.location.href = this.$baseurl
      } else if (response.data.status == 400002) {
        // this.$router.push({
        //   path:'/wuquanxian'
        // })
      }
      resolve(response);
    }).catch(err => {
      reject(err)
      this.$Notice.error({
        title: '提示',
        desc: '接口出问题了，请联系开发人员！'
      })
    })
  })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data = {},cancelToken = {}) {
  return new Promise((resolve, reject) => {
    data.data_scope = sessionStorage.getItem("data_scope")
    let obj = {
      url: url,
      method: 'post',
      data: data,
      headers: {
        'Authorization': sessionStorage.getItem("token"),
        'Content-Type': 'application/json;charset=utf-8',
      },
    }
    if(cancelToken != {}) obj.cancelToken = cancelToken.source
    axios(obj).then((response) => {
      if (response.data.status == 400001) {
        window.location.href = this.$baseurl
      } else if (response.data.status == 400002) {
        console.log(response.data)
        // this.$router.push({
        //   path:'/wuquanxian'
        // })
      }
      resolve(response);
    }, err => {
      if(cancelToken != {}) return
      this.$Notice.error({
        title: '提示',
        desc: '接口出问题了，请联系开发人员！'
      })
      reject(err)
    })
   
    // axios({
    //   url: url,
    //   method: 'post',
    //   data: data,
    //   headers: {
    //     'Authorization': sessionStorage.getItem("token"),
    //     'Content-Type': 'application/json;charset=utf-8',
    //   },
    // }).then((response) => {
    //   if (response.data.status == 400001) {
    //     window.location.href = this.$baseurl
    //   } else if (response.data.status == 400002) {
    //     console.log(response.data)
    //     // this.$router.push({
    //     //   path:'/wuquanxian'
    //     // })
    //   }
    //   resolve(response);
    // }, err => {
    //   this.$Notice.error({
    //     title: '提示',
    //     desc: '接口出问题了，请联系开发人员！'
    //   })
    //   reject(err)
    // })
  })
}


/**
 * 封装两种请求方式的请求
 * url
 * data
 *  接口请求的方式 post get 这两种方式
 */

export function methods(url, data = {}) {
  return new Promise((resolve, reject) => {
    data.data_scope = sessionStorage.getItem("data_scope")
    // var params=qs.stringify(data);
    axios({
      url: url,
      method: 'post',
      data: data,
      responseType: 'blob',
      headers: {
        'Authorization': sessionStorage.getItem("token"),
        'Content-Type': 'application/json;charset=utf-8',
      },
    }).then((response) => {
      if (response.data.status == 400001) {
        window.location.href = this.$baseurl
      } else if (response.data.status == 400002) {
        this.$Notice.warning({
          title: '提示',
          desc: response.data.msg
        })
      }
      resolve(response);
    }, err => {
      reject(err)
      this.$Notice.error({
        title: '提示',
        desc: '接口出问题了，请联系开发人员！'
      })
    })
  })
}


